package tema13.Ejercicio11;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Ej11 extends JFrame implements ActionListener {
	private String titulo = "Ventana Ejemplo 05";
	private int ancho = 450;
	private int alto = 250;
	private int anchoEscritorio, altoEscritorio, x, y;

	private JPanel jp, botones, botones2;
	private JTextField t1, t2;
	private JButton boton, boton2, boton3, boton4;
	public JLabel mensaje;

	public Ej11() {
		inicializar();
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		// getText()
		if (e.getActionCommand().equals("1")) {
			mensaje.setText(t1.getText());
		}

		else if (e.getActionCommand().equals("2")) {
			mensaje.setText(t2.getText());
		}

		else if (e.getActionCommand().equals("3")) {
			mensaje.setText(t1.getText() + " " + t2.getText());
		}

		else if (e.getActionCommand().equals("4")) {
			mensaje.setText(t2.getText() + " " + t1.getText());
		}

		else {
			mensaje.setText("Le has dado a enter.");
		}

	}

	private void inicializarPanel() {
		jp = new JPanel(new GridLayout(1, 2));

		t1 = new JTextField(30);
		t1.addActionListener(this);
		t2 = new JTextField(30);
		t2.addActionListener(this);

		jp.add(t1);
		jp.add(t2);

		boton = new JButton("Uno");
		boton.addActionListener(this);
		boton.setActionCommand("1");

		boton2 = new JButton("Dos");
		boton2.addActionListener(this);
		boton2.setActionCommand("2");

		boton3 = new JButton("Uno => dos");
		boton3.addActionListener(this);
		boton3.setActionCommand("3");

		boton4 = new JButton("Dos => uno");
		boton4.addActionListener(this);
		boton4.setActionCommand("4");

		botones = new JPanel();
		botones.setLayout(new GridLayout(1, 2));

		botones2 = new JPanel();
		botones2.setLayout(new GridLayout(1, 2));

		botones.add(boton);
		botones.add(boton2);
		botones2.add(boton3);
		botones2.add(boton4);

		mensaje = new JLabel();

		add(mensaje);
		add(jp);
		add(botones);
		add(botones2);

	}

	private void inicializar() {
		setTitle(titulo);
		setSize(ancho, alto);
		inicializarUbicacion();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new GridLayout(4, 1));
		inicializarPanel();
	}

	private void inicializarUbicacion() {
		altoEscritorio = Toolkit.getDefaultToolkit().getScreenSize().height;
		anchoEscritorio = Toolkit.getDefaultToolkit().getScreenSize().width;
		y = (altoEscritorio / 2) - (alto / 2);
		x = (anchoEscritorio / 2) - (ancho / 2);
		setLocation(x, y);
	}

	public static void main(String[] args) {
		new Ej11();
	}

}
